define(['jsonpack', './../initial', './Layer'], function (jsonpack) {
    var app = angular.module('App');
    app.factory('Project.Layer', ['$http', '$q', 'Layer', function ($http, $q, Layer) {

        var findMachedPoint = function (pointsGeoJson, location, ringNumber) {
            var matches = pointsGeoJson.features.filter(function (item) {
                return item.properties.RingNumber.toString() === ringNumber;
            });
            if (matches.length === 0) return null;
            else if (matches.length === 1) return matches[0];
            else {
                matches = matches.filter(function (item) {
                    return item.properties.Location === location;
                });
                return matches.length ? matches[0] : null;
            }
        };
        return {
            getLayer: function () {
                var defer = $q.defer();
                var _this = this;
                if (this.layerList && this.layerList.length > 0) {
                    defer.resolve(this.layerList);
                } else {
                    this.layerList = [];
                    $http.get('/metro/project/layerList', {
                        params: {
                            prjId: _this.prjId
                        }
                    }).success(function (data) {
                        var featureGroup = L.featureGroup();
                        data.forEach(function (item, index) {
                            var l = angular.extend(new Layer(), item);
                            l.layer = jsonpack.unpack(item.layer);
                            var reg = /ringnumber\w*/;
                            //匹配含有ringnumber的图层,在这些图层中为点赋值地理信息
                            if (reg.test(l.title.toLowerCase())) {
                                _this.parseGeoInfoForEachPoint(l.layer); //赋值地理位置信息
                            } else {
                                //只有不是ringnumber的时候才会推入项目的layers列表
                                _this.layerList.push(l);
                                featureGroup.addLayer(L.geoJson(l.layer));
                            }
                        });
                        var bounds = featureGroup.getBounds();
                        _this.projectMapCenter = bounds.getCenter();
                        var width = bounds.getEast() - bounds.getWest();
                        var height = bounds.getNorth() - bounds.getSouth();
                        _this.zoomRatio = 300 / Math.max(width, height);
                        defer.resolve(_this.layerList);
                    });
                }
                return defer.promise;
            },
            /** 为每一个监测点赋值布点图上的地理信息 */
            parseGeoInfoForEachPoint: function (geoLayerData) {
                var prjItem = this;
                //find project map center

                //parse geometry info to each monitor point
                var pointsGeoJson = geoLayerData;
                prjItem.monitorPoints.forEach(function (mPoints) {
                    mPoints.locationPoints && mPoints.locationPoints.forEach(function (lpts) {
                        for (var t = 0; t < lpts.points.length; t++) {
                            var pt = lpts.points[t];
                            //服务器里没配置ringnumber的,默认为none
                            if (!lpts.isRingNumberConfig) {
                                console.log(" location:%s name:%s ringnumber is null, config it.", pt.name, pt.ringNumber, lpts.location);
                                lpts.isRingNumberConfig = false;
                                break;
                            } else {
                                if (pt.geometry) continue;
                                var matchPt = findMachedPoint(pointsGeoJson, lpts.location, pt.ringNumber);
                                try {
                                    if (matchPt) {
                                        pt.geometry = matchPt.geometry.coordinates;
                                        if (matchPt.properties.Visible && matchPt.properties.Visible === "0") {
                                            //是否在地图上显示
                                            pt.visible = false;
                                        } else {
                                            pt.visible = true;
                                        }
                                    } else {
                                        pt.visible = false;
                                    }
                                } catch (err) {
                                    console.log("fail on parse geo info at name:%s ring:%s location:%s", pt.name, pt.ringNumber, lpts.location);
                                    console.log("mainly because this ringnumber is not generated in ringnumber.json layer, verify it.");
                                    lpts.isRingNumberConfig = false;
                                    break;
                                }
                            }

                        }
                    });
                });
            }
        };
    }]);
});

